﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebComponents;
using WebComponents.Data;
using WebComponents.Security;
using GBIT.Web;
using System.Collections.Generic;

public partial class AddFile : UserControlBase {
  int FileID = -1;
  protected void Page_Load(object sender, EventArgs e) {
    FileID = ConvertType.ToInt(Request.Params["FileID"]);
    this.Title = "Album hình ảnh";
    this.TaskTitle = "Thêm mới";
    if (!IsPostBack) {
      LoadAlbum();
    }
    if (FileID > 0) {
      if (!IsPostBack) {
        LoadFileByFileID(FileID);
        this.TaskTitle = "Cập nhật";
      }
    } else
      txtViTri.Text = FileController.MaxOrderId().ToString();
    this.SaveEnabled = true;
  }
  protected void LoadAlbum() {
    this.cmbAlbum.Items.Clear();
    this.cmbAlbum.Items.Add("");
    Global.LoadCategory("vi-VN", ref cmbAlbum, CatetoryType.File);    
  }
  protected void LoadFileByFileID(int FileID) {
    FileInfo obj = FileController.GetByFileID(FileID);
    txtTitle.Text = obj.Title;
    txtViTri.Text = obj.OrderId.ToString();
    ImagePath.ImagePath = obj.FileURL;
    ImagePath.ImageUrl = obj.FileURL;
  }
  protected override void OnSaveRecord() {
    FileInfo obj = null;
    if (FileID > 0)
      obj = FileController.GetByFileID(FileID);
    if (obj == null)
      obj = new FileInfo();
    obj.Title = txtTitle.Text.Trim();
    obj.CateID = ToInt(cmbAlbum.SelectedValue);
    obj.OrderId = ConvertType.ToInt(txtViTri.Text);
    obj.FileURL = ImagePath.ImagePath;
    obj.FileType = ImagePath.FileType;
    obj.FileSize = ImagePath.FileSize;
    if (FileID > 0) {
      FileController.Update(obj);
    } else {
      obj.AddedDate = DateTime.Now;
      obj.IsActive = true;
      FileController.Insert(obj);
    }
    Security.ShowClientMessage(Page, "Lưu trữ thông tin hình ảnh thành công!", string.Format("?mod={0}", WebComponents.ConvertType.Encode("TrangChu/AlbumHinh.ascx")));
  }
}
